<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%@include file="../common/CheckLoginBackend.jsp" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:set value="${pageContext.request.contextPath}" var="ctx"/>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <link type="text/css" rel="stylesheet" href="../statics/css/style.css"/>
    <title>二组牛逼</title>
    <script type="text/javascript" src="../statics/js/jquery.js"></script>
    <script type="text/javascript">
        var contextPath = "${ctx}";

        function fan(pageNo) {
            window.location.href = "b_product?pageNo=" + pageNo;
        }

        function yiji() {
            //获取当前选中一级分类的id
            var $id = $("#productCategoryLevel1").val();
            //调用ajax获得二级分类
            $.ajax({
                url: "ProductListServlet",
                method: "post",
                data: {
                    action: "category",
                    category: "two",
                    id: $id
                },
                dataType: "json",
                success: function (data) {
                    var erji = "<option value='0' selected>请选择...</option>";
                    for (var i = 0; i < data.length; i++) {
                        erji += "<option value='" + data[i].id + "'>" + data[i].name
                            + "</option>";
                    }
                    $("#productCategoryLevel2").html(erji);
                }
            });
            //清空三级下拉框
            $("#productCategoryLevel3").html(
                "<option value='-1' selected>请选择...</option>");
        }

        function erji() {
            //获取当前选中一级分类的id
            var $id = $("#productCategoryLevel2").val();
            //调用ajax获得二级分类
            $.ajax({
                url: "ProductListServlet",
                method: "post",
                data: {
                    action: "category",
                    category: "three",
                    id: $id
                },
                dataType: "json",
                success: function (data) {
                    var sanji = "<option value='0' selected>请选择...</option>";
                    for (var i = 0; i < data.length; i++) {
                        sanji += "<option value='" + data[i].id + "'>" + data[i].name
                            + "</option>";
                    }
                    $("#productCategoryLevel3").html(sanji);
                }
            });
        }

        $(document).ready(function () {
            $("#productAdd").submit(function () {
                //获取商品的三个分类下拉框值
                var pc1 = $("#productCategoryLevel1").val();
                var pc2 = $("#productCategoryLevel2").val();
                var pc3 = $("#productCategoryLevel3").val();
                if (pc1 == 0 || pc2 == 0 || pc3 == 0) {
                    window.alert("请选择所属1-3级分类");
                    return false;
                }
                //获取商品单价
                var price = $("#price").val();
                if (isNaN(Number(price)) || price <= 0) {
                    window.alert("请输入正确的商品价格");
                    return false;
                }
                //获取商品库存
                var stock = $("#stock").val();
                if (isNaN(Number(stock)) || stock < 0) {
                    window.alert("请输入正确的库存");
                    return false;
                }
            });
        });
    </script>
</head>
<body>
<%@include file="../common/Admin_Header.jsp" %>
<div class="i_bg bg_color">
    <!--Begin 用户中心 Begin -->
    <div class="m_content">
        <%@include file="../common/Admin_left.jsp" %>
        <div class="m_right" id="content">
            <div class="mem_tit">
                <c:if test="${product!=null}">修改商品</c:if>
                <c:if test="${product==null}">添加商品</c:if>
            </div>

            <br/>
            <form action="ProductListServlet?action=addUpdate" method="post"
                  enctype="multipart/form-data" id="productAdd">
                <table border="0" class="add_tab" style="width:930px;"
                       cellspacing="0" cellpadding="0">
                    <tr>
                        <td width="135" align="right">一级分类</td>
                        <td colspan="3" style="font-family:'宋体';"><select
                                name="categoryLevel1Id" style="background-color:#f6f6f6;"
                                id="productCategoryLevel1" onchange="yiji()">
                            <option value="0" selected>请选择...</option>
                            <c:forEach items="${list1}" var="pc1" varStatus="v">
                                <option value="${pc1.id}"
                                        <c:if test="${pc1.id==product.categoryLevel1Id}">selected</c:if>>${pc1.name}</option>
                            </c:forEach>
                        </select>
                        </td>
                    </tr>
                    <tr>
                        <td width="135" align="right">二级分类</td>
                        <td><select name="categoryLevel2Id"
                                    style="background-color:#f6f6f6;" id="productCategoryLevel2"
                                    onchange="erji()">
                            <option value="0" selected>请选择...</option>
                            <c:if test="${product!=null}">
                                <c:forEach items="${list2}" var="pc2" varStatus="v">
                                    <option value="${pc2.id}"
                                            <c:if test="${pc2.id==product.categoryLevel2Id}">selected</c:if>>${pc2.name}</option>
                                </c:forEach>
                            </c:if>
                        </select>
                        </td>
                    </tr>
                    <tr>
                        <td width="135" align="right">三级分类</td>
                        <td><select name="categoryLevel3Id"
                                    style="background-color:#f6f6f6;" id="productCategoryLevel3">
                            <option value="0" selected>请选择...</option>
                            <c:if test="${product!=null}">
                                <c:forEach items="${list3}" var="pc3" varStatus="v">
                                    <option value="${pc3.id}"
                                            <c:if test="${pc3.id==product.categoryLevel3Id}">selected</c:if>>${pc3.name}</option>
                                </c:forEach>
                            </c:if>
                        </select>
                        </td>
                    </tr>
                    <tr>
                        <td width="135" align="right">商品名称</td>
                        <td><input type="text"
                                   value="<c:if test="${product!=null}">${product.name}</c:if>"
                                   class="add_ipt" name="name" id="name" required/>（必填） <input
                                type="hidden" name="id"
                                value="<c:if test='${product!=null}'>${product.id}</c:if><c:if test='${product==null}'>-1</c:if>"
                                id="id"/>
                        </td>
                    </tr>
                    <tr>
                        <td width="135" align="right">商品图片</td>
                        <td><c:if test="${product!=null}">
                            <img src="../statics/productImg/${product.fileName}" width="50" height="50"/>
                            <input type="hidden" value="${product.fileName}"
                                   name="photoimg"/>
                        </c:if> <input type="file" class="text" name="photo" id="photo"/><span></span>
                        </td>
                    </tr>
                    <tr>
                        <td width="135" align="right">单价</td>
                        <td><input type="text"
                                   value="<c:if test="${product!=null}">${product.price}</c:if>"
                                   class="add_ipt" name="price" id="price" required/>
                        </td>
                    </tr>
                    <tr>
                        <td width="135" align="right">库存</td>
                        <td><input type="text"
                                   value="<c:if test="${product!=null}">${product.stock}</c:if>"
                                   class="add_ipt" name="stock" id="stock" required/>
                        </td>
                    </tr>
                    <tr>
                        <td width="135" align="right">描述</td>
                        <td><textarea name="description">
									<c:if test="${product!=null}">${product.description}</c:if>
								</textarea>
                        </td>
                    </tr>
                    <tr>
                        <td></td>
                        <td><c:if test="${product==null }">
                            <input type="submit" value="商品上架" class="s_btn">
                        </c:if> <c:if test="${product!=null }">
                            <input type="submit" value="确定修改" class="s_btn">
                        </c:if>
                        </td>
                    </tr>
                </table>
            </form>
        </div>
    </div>
</div>
</body>
</html>
